package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Created by 80002023 on 2016/8/1.
 */
public class LoadingDemo extends Game {
    AssetManager manager;
    BitmapFont font;
    SpriteBatch batch;
    BitmapFont font2;
    TextureAtlas tex2;
    TextureAtlas map1;
    Texture tex1;
    Texture progressBar_BG;
    Texture progressBar_FT;
    TextureRegion region;
    float progress = 0;

    boolean diagnosed = false;

    @Override
    public void create() {
        batch = new SpriteBatch();
        font = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);
        manager = new AssetManager();

        progressBar_BG = new Texture("data/ProgressBar.png");
        progressBar_FT = new Texture("data/GreenBar.png");
        region = new TextureRegion(progressBar_BG, 0, 0, 512, 64);

        load();
    }

    public void load() {
        tex1 = new Texture("data/animation.png");
        tex2 = new TextureAtlas(Gdx.files.internal("data/pack"));
        map1 = new TextureAtlas(Gdx.files.internal("map1.pack"));
        font2 = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), false);
        // 将资源加到预加载队列。
        manager.load("data/animation.png", Texture.class);
        manager.load("data/pack1.png", Texture.class);
        manager.load("data/pack", TextureAtlas.class);
        manager.load("data/verdana39.png", Texture.class);
        manager.load("data/verdana39.fnt", BitmapFont.class);

//        manager.load("map1.pack", TextureAtlas.class);
//        manager.load("map1.png", Texture.class);
//        manager.load("map1.txm", TiledMap.class);
    }

    public void unload() {
        tex1.dispose();
        tex2.dispose();
        font2.dispose();
        // 卸载
        manager.unload("data/animation.png");
        manager.unload("data/pack1.png");
        manager.unload("data/pack");
        manager.unload("data/verdana39.png");
        manager.unload("data/verdana39.fnt");
//        manager.unload("map1.pack");
//        manager.unload("map1.png");
    }

    @Override
    public void render() {

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        boolean result = manager.update();

        if (result & !diagnosed) {
            diagnosed = true;
            unload();
            load();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            diagnosed = false;
        }
        {

            batch.begin();

            if (manager.isLoaded("data/animation.png"))
                batch.draw(manager.get("data/animation.png", Texture.class), 100, 100);
            if (manager.isLoaded("data/verdana39.png"))
                batch.draw(manager.get("data/verdana39.png", Texture.class), 300, 100);
            if (manager.isLoaded("data/pack"))
                batch.draw(manager.get("data/pack", TextureAtlas.class).findRegion("particle-star"), 164, 100);
            if (manager.isLoaded("map1.pack"))
                batch.draw(manager.get("map1.pack", TextureAtlas.class).findRegion("map1"), 84, 100);
            if (manager.isLoaded("map1.png"))
                batch.draw(manager.get("map1.png", Texture.class), 284, 100);
            if (manager.isLoaded("data/verdana39.fnt"))
                manager.get("data/verdana39.fnt", BitmapFont.class).draw(batch, "This is a test", 100, 200);
            if (manager.isLoaded("data/back.ogg", Sound.class))
                manager.get("data/back.ogg", Sound.class).play();

            progress = manager.getProgress() * 700;
//        if (manager.getProgress() % 10 == 0) {
            if (result) {
                font.draw(batch, "Progress: " + (100) + "%", 520, 60);
            } else {
                font.draw(batch, "Progress: " + (int) (manager.getProgress() * 100) + "%", 520, 60);
            }

            Gdx.app.log("Loading", "finish: " + manager.getProgress());
//        }

            batch.draw(progressBar_FT, 40, 0, progress, 64);

            batch.draw(region, 0, 20);

            batch.end();
        }
    }
}
